package com.lzj.test3;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

public class TrackerWindowManager {

    private Context context;
    private WindowManager windowManager;
    private View FloatingView;
    private static WindowManager.LayoutParams LAYOUT_PARAMS;

    public TrackerWindowManager(Context context) {
        this.context = context;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    static {
        WindowManager.LayoutParams params=new WindowManager.LayoutParams();
        params.x=0;
        params.y=0;
        params.width=WindowManager.LayoutParams.WRAP_CONTENT;
        params.height=WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity= Gravity.LEFT|Gravity.TOP;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        params.format= PixelFormat.RGBA_8888;
        params.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        LAYOUT_PARAMS=params;
    }

    public void addView(){
        if (FloatingView==null){
            FloatingView=new FloatingView(context);
            FloatingView.setLayoutParams(LAYOUT_PARAMS);
            windowManager.addView(FloatingView,LAYOUT_PARAMS);
        }
    }

    public void removeView(){
        if (FloatingView!=null){
            windowManager.removeView(FloatingView);
            FloatingView=null;
        }
    }
}
